package com.swz.dcrm.ui.car;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.adpter.InsuranceAdapter;
import com.swz.dcrm.adpter.MultiItemTypeAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Insurance;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.CarManagementViewModel;

import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class InsuranceFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_total_fee)
    TextView tvTotalFee;

    private InsuranceAdapter insuranceAdapter;
    @Inject
    CarManagementViewModel carManagementViewModel;
    public static InsuranceFragment newInstance() {

        return new InsuranceFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
        carManagementViewModel.getInsurances().observe(this, listBaseResponse -> {
            if(listBaseResponse.isSuccess()){
                insuranceAdapter=new InsuranceAdapter(getContext(),listBaseResponse.getData());
                insuranceAdapter.setOnItemClickListener(onItemClickListener);
                if(carManagementViewModel.getSelectedInsurance().getValue()!=null){
                    insuranceAdapter.setSelected(carManagementViewModel.getSelectedInsurance().getValue());
                }
                rv.setAdapter(insuranceAdapter);
            }
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_insurance;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        smartRefreshLayout.setEnableLoadmore(false);
        smartRefreshLayout.setEnableRefresh(false);
        title.setText(getString(R.string.insurance_title));
        tvRight.setText(getString(R.string.save));
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        return true;
    }

    private MultiItemTypeAdapter.OnItemClickListener onItemClickListener=new MultiItemTypeAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                tvTotalFee.setText(insuranceAdapter.getSelectedTotalFee());
        }

        @Override
        public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
            return false;
        }
    };
    @OnClick
    public void save(){
        carManagementViewModel.getSelectedInsurance().setValue(insuranceAdapter.getSelected());
        backClick();
    }
    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
}
